package org.linlinjava.litemall.web.service;

import lombok.RequiredArgsConstructor;
import org.apache.ibatis.type.LocalDateTimeTypeHandler;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallSettlement;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.domain.LumiereCalculateSettlement;
import org.linlinjava.litemall.db.service.LitemallSettlementService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.db.service.LumiereCalculateSettlementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import static org.linlinjava.litemall.web.util.WebResponseCode.FIRST_SETTLEMENT_ALREADY_EXIST;

@Service
@RequiredArgsConstructor
public class WebCalculateSettlementService {
    private final LumiereCalculateSettlementService lumiereCalculateSettlementService;
    public Object add(BigDecimal totalCash,BigDecimal curDeserveCash, Integer shopId,Integer userId,String denomination){
        //获取上一次结算的金额
        LumiereCalculateSettlement settlement = getLastSettlement(shopId);

        //上次结算时钱箱中的金额
        BigDecimal preTotalCash =   settlement == null ? BigDecimal.ZERO : settlement.getTotalCash();
        BigDecimal deserveCash = preTotalCash.add(curDeserveCash);
        LumiereCalculateSettlement settle = new LumiereCalculateSettlement();
        settle.setDeserveCash(deserveCash);
        settle.setTotalCash(totalCash);
        settle.setUserId(userId);
        settle.setShopId(shopId);
        settle.setAddTime(LocalDateTime.now());
        settle.setUpdateTime(LocalDateTime.now());
        settle.setDeleted(false);
        settle.setDenomination(denomination);
        lumiereCalculateSettlementService.save(settle);

        return ResponseUtil.ok("结算成功");
    }

    public Object getOriginalCash(Integer shopId){
        LumiereCalculateSettlement lastSettlement = getLastSettlement(shopId);
        return lastSettlement != null ? lastSettlement.getTotalCash() : BigDecimal.ZERO;
    }

    public Object insertCashAfterOpen(BigDecimal amount,Integer shopId,Integer userId){
        LumiereCalculateSettlement settlement = getLastSettlement(shopId);

        if(settlement != null){
            return ResponseUtil.fail(FIRST_SETTLEMENT_ALREADY_EXIST,"当日第一次结算已经存在");
        }
        settlement = new LumiereCalculateSettlement();
        settlement.setTotalCash(amount);
        settlement.setDeserveCash(amount);
        settlement.setShopId(shopId);
        settlement.setUserId(userId);
        settlement.setAddTime(LocalDateTime.now());
        settlement.setUpdateTime(LocalDateTime.now());
        settlement.setDeleted(false);
        lumiereCalculateSettlementService.add(settlement);
        return ResponseUtil.ok("插入成功");
    }


    public LumiereCalculateSettlement getLastSettlement(Integer shopId){
        //获取上一次结算的金额
        List<LumiereCalculateSettlement> settlements = lumiereCalculateSettlementService.queryLast(shopId);
        LumiereCalculateSettlement settlement = null;
        if(settlements != null && settlements.size() > 0 &&  settlements.get(0) != null){
            settlement = settlements.get(0);
        }

        return settlement;
    }

}
